<?php

/**
 * Admin_System File
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
 * @category Admin_System
 * @package  Admin_System_Package
 * @author Johari <joharijumali@gmail.com>  
 */

use Admin\Models\Modul\Step as Step;
use Admin\Models\Modul\Page as Page;
use Admin\Models\User\Role as Role;
use Admin\Models\Navigation\Header as Header;
use Admin\Models\Navigation\Link as Link;
use Admin\Libraries\Menu as Menu;
use Logger as Logger;

/**
 * Admin_Data class
 * 
 * System Management Class
 *
 * @category Admin_Data_Controller
 * @package  Admin_Data_Package
 * @author Johari <joharijumali@gmail.com>   
 */

class Admin_System_Controller extends Admin_Base_Controller
{

	/**
     * Function get_index
	 *
	 * @return get_index.
     */
    public function get_index()
	{
    	return View::make('admin::system.index');
    }
	
	/**
     * Navigation Setup function
     * Component : navigate
     * Method 	 : ajax post
	 *
	 * @return void.
     */
    public function get_navigate()
	{
        $data['render'] = Menu::navTree();
        $data['headerlist'] = $list = Header::listheader();
        $data['page'] = $listpage = Page::listpages();
        $data['pagelist'] = $listpage = Page::listAvailpages();

        return View::make('admin::system.navigate', $data);
    }
	
	/**
     * Function post_navigate
	 *
	 * @return post_navigate.
     */
    public function post_navigate()
    {
        $contents = Input::get();

        foreach ($contents['module'] as $key => $value) {
            $module = Header::find($value);
            $module->snh_step = $key+1;
            $module->save();
        }

        foreach ($contents['parent'] as $key => $value) {
            $module = Link::find($value);
            $module->snp_parent_step = $key+1;
            $module->save();
        }

        Log::write('System', 'Update Navigation by <code>'.Auth::user()->username.'</code>');

        return Redirect::to('admin/system/navigate');
    }
	
	/**
     * Navigation utilities function
	 *
	 * @return json data.
     */
    public function get_navchild()
	{
        $input = Input::get();

        $navigation = Link::find($input['navid']);
        $nav = Header::find($navigation->snh_id);
        $parent = Page::find($navigation->smp_id);

        $data['navheaderid'] = $navigation->snh_id;
        $data['module'] = $nav->snh_header;
        $data['parentid'] = $navigation->smp_id;
        $data['parent'] = $parent->smp_action_alias;

        return json_encode($data);
    }
	
	/**
     * Function get_resetnavdata
	 *
	 * @return get_resetnavdata.
     */
    public function get_resetnavdata()
	{
        $data['navheaderid'] = $list = Header::listheader();
        $data['modulpageid'] = $listpage = Page::listAvailpages();

        return json_encode($data);
    }
	
	/**
     * Function post_setmodule
	 *
	 * @return post_setmodule.
     */
    public function post_setmodule()
	{
        $input = Input::get();

        $module = new Header;
        $module->snh_header = $input['navheader'];
        $module->save();

        Log::write('System', 'Add New Navigation Module '.$input['navheader'].' by <code>'.Auth::user()->username.'</code>');

        return Menu::navTree();
    }

	/**
     * Function post_setpage
	 *
	 * @return post_setpage.
     */
    public function post_setpage()
	{
        $input = Input::get();

        $pages = new Link;
        $pages->snh_id = $input['navheaderid'];
        $pages->smp_id = $input['modulpageid'];
        $pages->save();


        $header = Header::find($input['navheaderid'])->snh_header;
        $page = Page::find($input['modulpageid'])->smp_action_alias;

        Log::write('System', 'Add New Navigation Page '.$header.' / '.$page.' by <code>'.Auth::user()->username.'</code>');

        return Menu::navTree();
    }
	
	/**
     * Function post_setchild
	 *
	 * @return post_setchild.
     */
    public function post_setchild()
	{
        $input = Input::get();

        $pages = new Link;
        $pages->snh_id = $input['navheaderid'];
        $pages->snp_parent = $input['parentid'];
        $pages->smp_id = $input['modulpageid'];
        $pages->save();

        $header = Header::find($input['navheaderid'])->navheader;
        $parent = Page::find($input['parentid'])->actionalias;
        $subpage = Page::find($input['modulpageid'])->actionalias;

        Log::write('System', 'Add New Navigation Sub Page '.$header.'/'.$parent.'/'.$subpage.' by <code>'.Auth::user()->username.'</code>');

        return Menu::navTree();
    }

	/**
     * Function post_deletepages
	 *
	 * @return post_deletepages.
     */
    public function post_deletepages()
	{
        $input = Input::get();

        Link::find($input['id'])->delete();

        Log::write('System', 'Delete Navigation Page '.$input['id'].' by <code>'.Auth::user()->username.'</code>');

        return Menu::navTree();
    }
	
	/**
     * Function post_deletemodule
	 *
	 * @return post_deletemodule.
     */
    public function post_deletemodule()
	{
        $input = Input::get();

        $header = Header::find($input['id']);

        $head = $header->snh_header;

        $header->delete();

        $desc = Link::where('snh_id', '=', $input['id'])->get();

        Link::where('snh_id', '=', $input['id'])->delete();
        
        Log::write('System', 'Delete Navigation Module '.$head.' by <code>'.Auth::user()->username.'</code>');

        foreach ($desc as $key => $value) {
            $page = Page::find($value->smp_id)->smp_action_alias;
            Log::write('System', 'Delete Navigation Page '.$head.'/'.$page.' by <code>'.Auth::user()->username.'</code>');
        }

        return Menu::navTree();
    }
	
	/**
     * System Logger function
	 *
	 * @return void.
     */
    public function get_logger()
	{
        $logs = Logger::get_log(date('Y-m-d'));
        $data['currentselection'] = date('d-m-Y');
        $data['logs'] = !empty($logs) ? $logs : array('No Log Data Recorded');
    	
    	return View::make('admin::system.logs', $data);
    }

	/**
     * Function post_logger
	 *
	 * @return post_logger.
     */
    public function post_logger()
	{
        $input = Input::get();

        $logs = Logger::get_log(date('Y-m-d', strtotime($input['date'])));

        $data['currentselection'] = date('d-m-Y', strtotime($input['date']));
        $data['logs'] = !empty($logs) ? $logs : array('No Log Data Recorded');

        return View::make('admin::system.logs', $data);
    }
	 
	/**
     * System Configuration function
     * Component : sysConfig
     * Method 	 : post
	 *
	 * @return void.
     */
    public function get_sysConfig()
	{
        $data = array();

        $data['pagination'] = Config::get('system.pagination');
        $data['emailnotices'] = Config::get('system.emailnotices');
        $data['loginexpired'] = Config::get('session.lifetime');

        foreach ($data as $key => $value) {
            if ($value != null) {
                $data[$key.'_cond'] = 'success';
            } else {
                $data[$key.'_cond'] = 'warning';
            }
        }

        return View::make('admin::system.config', $data);
    }
	
	/**
     * Function post_sysConfig
	 *
	 * @return post_sysConfig.
     */
    public function post_sysConfig()
	{
        $input = Input::get();

        $emailnotices = ($input['emailnotices'] == 0)? 'false':'true';
        $confignotices = (Config::get('system.emailnotices') == 0)? 'false':'true';

        $pagination = ($input['pagination'] == 0 || $input['pagination'] == null)? 20 : $input['pagination'];
        $loginexpired = ($input['loginexpired'] == 0 || $input['loginexpired'] == null)? 15 : $input['loginexpired'];

        $syspath = path('app').'config/system'.EXT;

        $currentconfig = File::get($syspath);

        $config = str_replace("'pagination' => ".Config::get('system.pagination').",", "'pagination' => {$pagination},", $currentconfig, $count);
        $config = str_replace("'emailnotices' => {$confignotices},", "'emailnotices' => {$emailnotices},", $config, $count);

        File::put($syspath, $config);

        $sespath = path('app').'config/session'.EXT;

        $sesconfig = File::get($sespath);

        $sesconfig = str_replace("'lifetime' => ".Config::get('session.lifetime').",", "'lifetime' => {$loginexpired},", $sesconfig, $count);

        File::put($sespath, $sesconfig);

        $data['pagination'] = $pagination;
        $data['emailnotices'] = $input['emailnotices'];
        $data['loginexpired'] = $loginexpired;

        foreach ($data as $key => $value) {
            if ($value != 0) {
                $data[$key.'_cond'] = 'success';
            } else {
                $data[$key.'_cond'] = 'warning';
            }
        }

        return View::make('admin::system.config', $data);
    }
	
	/**
     * Jabatan / Bahagian / Unit management function
     * Component : department
     * Method 	 : ajax post
	 *
	 * @return void.
     */
    public function get_department()
	{
        return View::make('admin::system.department');
    }

}

?>